package com.xzzz.irda.guardcat.core.metric.threadpool;


import cn.hutool.core.collection.CollectionUtil;
import com.xzzz.common.base.util.JmxUtil;

import javax.management.ObjectName;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 自定义线程池的信息
 *
 * @author xzzz
 * @since 1.0.0
 */
public class CustomThreadPoolInfo {

    /**
     * 获取线程池名称
     */
    private String poolName;

    /**
     * 线程池中正在主动执行任务的线程的大致数量
     */
    private int activeCount;

    /**
     * 线程池中已执行完成的大致任务总数
     */
    private long completedTaskCount;

    /**
     * 线程池中核心线程数量
     */
    private int corePoolSize;

    /**
     * 线程池中曾经创建过的最大线程数量
     */
    private int largestPoolSize;

    /**
     * 线程池中允许的最大线程数量
     */
    private int maximumPoolSize;

    /**
     * 线程池中当前线程数
     */
    private int currentPoolSize;

    /**
     * 线程池中需要执行的任务数量
     */
    private long taskCount;

    /**
     * 线程池中队列中的任务数量
     */
    private int queueTaskCount;

    /**
     * 线程池中队列的剩余容量
     */
    private int queueRemainingCapacity;

    /**
     * 线程最大耗时
     */
    private long maxCostTime;

    /**
     * 线程最小耗时
     */
    private long minCostTime;

    /**
     * 总耗时
     */
    private long totalCostTime;


    public static List<CustomThreadPoolInfo> getCustomThreadPoolInfos() {
        Set<ObjectName> threadPools = JmxUtil.getObjectNameSet(JmxUtil.CUSTOM_THREAD_POOL);

        if (CollectionUtil.isEmpty(threadPools)) {
            return null;
        }

        List<CustomThreadPoolInfo> threadPoolInfos = new ArrayList<>();
        CustomThreadPoolInfo threadPoolInfo;
        for (ObjectName threadPool : threadPools) {
            threadPoolInfo = new CustomThreadPoolInfo();

            // 线程池中正在主动执行任务的线程的大致数量
            threadPoolInfo.setPoolName(JmxUtil.getMbeanStrVal(threadPool, "poolName"));

            // 线程池中正在主动执行任务的线程的大致数量
            threadPoolInfo.setActiveCount(JmxUtil.getMbeanIntegerVal(threadPool, "activeCount"));

            // 线程池中已执行完成的大致任务总数
            threadPoolInfo.setCompletedTaskCount(JmxUtil.getMbeanLongVal(threadPool, "completedTaskCount"));

            // 线程池中核心线程数量
            threadPoolInfo.setCorePoolSize(JmxUtil.getMbeanIntegerVal(threadPool, "corePoolSize"));
            // 线程池中曾经创建过的最大线程数量
            threadPoolInfo.setLargestPoolSize(JmxUtil.getMbeanIntegerVal(threadPool, "largestPoolSize"));
            // 线程池中允许的最大线程数量
            threadPoolInfo.setMaximumPoolSize(JmxUtil.getMbeanIntegerVal(threadPool, "maximumPoolSize"));
            // 线程池中当前线程数
            threadPoolInfo.setCurrentPoolSize(JmxUtil.getMbeanIntegerVal(threadPool, "poolSize"));
            // 线程池中需要执行的任务数量
            threadPoolInfo.setTaskCount(JmxUtil.getMbeanLongVal(threadPool, "taskCount"));

            // 线程池中队列中的任务数量
            threadPoolInfo.setQueueTaskCount(JmxUtil.getMbeanIntegerVal(threadPool, "queueTaskCount"));
            // 线程池中队列的剩余容量
            threadPoolInfo.setQueueRemainingCapacity(JmxUtil.getMbeanIntegerVal(threadPool, "queueRemainingCapacity"));

            threadPoolInfos.add(threadPoolInfo);
        }
        return threadPoolInfos;
    }


    public String getPoolName() {
        return poolName;
    }

    public void setPoolName(String poolName) {
        this.poolName = poolName;
    }

    public int getActiveCount() {
        return activeCount;
    }

    public void setActiveCount(int activeCount) {
        this.activeCount = activeCount;
    }

    public long getCompletedTaskCount() {
        return completedTaskCount;
    }

    public void setCompletedTaskCount(long completedTaskCount) {
        this.completedTaskCount = completedTaskCount;
    }

    public int getCorePoolSize() {
        return corePoolSize;
    }

    public void setCorePoolSize(int corePoolSize) {
        this.corePoolSize = corePoolSize;
    }

    public int getLargestPoolSize() {
        return largestPoolSize;
    }

    public void setLargestPoolSize(int largestPoolSize) {
        this.largestPoolSize = largestPoolSize;
    }

    public int getMaximumPoolSize() {
        return maximumPoolSize;
    }

    public void setMaximumPoolSize(int maximumPoolSize) {
        this.maximumPoolSize = maximumPoolSize;
    }

    public int getCurrentPoolSize() {
        return currentPoolSize;
    }

    public void setCurrentPoolSize(int currentPoolSize) {
        this.currentPoolSize = currentPoolSize;
    }

    public long getTaskCount() {
        return taskCount;
    }

    public void setTaskCount(long taskCount) {
        this.taskCount = taskCount;
    }

    public int getQueueTaskCount() {
        return queueTaskCount;
    }

    public void setQueueTaskCount(int queueTaskCount) {
        this.queueTaskCount = queueTaskCount;
    }

    public int getQueueRemainingCapacity() {
        return queueRemainingCapacity;
    }

    public void setQueueRemainingCapacity(int queueRemainingCapacity) {
        this.queueRemainingCapacity = queueRemainingCapacity;
    }

    public long getMaxCostTime() {
        return maxCostTime;
    }

    public void setMaxCostTime(long maxCostTime) {
        this.maxCostTime = maxCostTime;
    }

    public long getMinCostTime() {
        return minCostTime;
    }

    public void setMinCostTime(long minCostTime) {
        this.minCostTime = minCostTime;
    }

    public long getTotalCostTime() {
        return totalCostTime;
    }

    public void setTotalCostTime(long totalCostTime) {
        this.totalCostTime = totalCostTime;
    }

    @Override
    public String toString() {
        return "ThreadPoolInfo{" +
                "poolName='" + poolName + '\'' +
                ", activeCount=" + activeCount +
                ", completedTaskCount=" + completedTaskCount +
                ", corePoolSize=" + corePoolSize +
                ", largestPoolSize=" + largestPoolSize +
                ", maximumPoolSize=" + maximumPoolSize +
                ", currentPoolSize=" + currentPoolSize +
                ", taskCount=" + taskCount +
                ", queueTaskCount=" + queueTaskCount +
                ", queueRemainingCapacity=" + queueRemainingCapacity +
                ", maxCostTime=" + maxCostTime +
                ", minCostTime=" + minCostTime +
                ", totalCostTime=" + totalCostTime +
                '}';
    }
}
